<?php
/* -----------------------------------------------------------------
 *                          Aveghe Installer
 * -----------------------------------------------------------------
 * Aveghe Installer: made your web application installation easy!
 * Copyright (C) 2008 Elia Contini
 * 
 * Aveghe Installer is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * Aveghe Installer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 */

//error_reporting (E_STRICT);
set_include_path(get_include_path().PATH_SEPARATOR.'../lib');

require_once('Zend/Loader.php');
require_once('../aveghe/conf/conf.php');

Zend_Loader::registerAutoload();

$avegheNS = new Zend_Session_Namespace('avegheNS');

if($_SERVER['REQUEST_METHOD'] == 'GET')
	{
		header('Location: index.php');
		exit;
	}

$user_login = $_POST['user_login'];
$user_email = $_POST['user_email'];
$user_password = $_POST['user_password'];
$user_password_check = $_POST['user_password_check'];

// data test here
if($user_password != $user_password_check)
	{
		header('Location: createUserForm.php');
		exit;
	}

$parameters = array(
					'host'     => DATABASE_HOST,
     				'username' => DATABASE_USERNAME,
    				'password' => DATABASE_PASSWORD,
    				'dbname'   => DATABASE_NAME,
				   );

$theme = "resources/themes/aveghe/template.xhtml";
$meta = "<meta http-equiv=\"Content-Type\" content=\"application/xhtml+xml; charset=UTF-8\" />\n";

if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml")) 
	header("Content-type: application/xhtml+xml");
else
	{
		header("Content-type: text/html");
		$meta = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n";
	}

try
	{
		// schema installation
    	$db = Zend_Db::factory('Pdo_Mysql', $parameters);
    	
    	$db->getConnection();
    	$result = $db->fetchAll("SELECT * FROM ".DATABASE_TABLE_PREFIX."users;");
    	
    	if( count($result) == 0)
    		{
    			$query  = "INSERT INTO ".DATABASE_TABLE_PREFIX."users (user_login, user_password, user_email) ";
    			$query .= "VALUES (\"".$user_login."\", \"".md5($user_password)."\", \"".$user_email."\");";
    			$db->getConnection()->exec($query);
    		}
    	
    	$db->closeConnection();
	}
catch (Zend_Db_Adapter_Exception $e)
	{
    	// perhaps a failed login credential, or perhaps the RDBMS is not running
    	print($e->getMessage());
	}
catch (Zend_Exception $e)
	{
    	// perhaps factory() failed to load the specified Adapter class
    	print($e->getMessage());
	}

$message       = '';
$content = <<< CONTENT

	<h2>Rapporto</h2>
	<p class="success">Utente creato con successo.</p>
	
CONTENT;
	
include($theme);
?>